﻿using CarAppDomain.CarType.Entities;
using CarAppDomain.Rule.Entities;
using CarAppDomain.Rule.IRepositories;
using CarAppDto.Rule.RuleConfigDtos;
using D.UtilSqlsugarOrm;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;

namespace CarAppDomain.Rule.Repositories
{
    public class RuleConfigEntityRepository : BaseRepository<RuleConfigEntity>, IRuleConfigEntityRepository
    {
        private readonly ISqlSugarClient sqlSugarClient;
        public RuleConfigEntityRepository(ISqlSugarClient db):base(db)
        {
            sqlSugarClient = db;
        }
        /// <summary>
        /// 根据vin查询规则
        /// </summary>
        /// <param name="VIN"></param>
        /// <returns></returns>

        public async Task<List<RepRuleConfig>>  GetRuleConfigByVINAsync(string VIN)
        {
            string sql = "";
            switch (((SqlSugar.SqlSugarScope)sqlSugarClient).CurrentConnectionConfig.DbType)
            {
                case DbType.MySql: sql = $"SELECT rule_config.*,brand.`Name` BrandEntityName FROM rule_config LEFT JOIN brand ON rule_config.BrandId=brand.Id WHERE @vin REGEXP RegularExpression; "; break;
                case DbType.Oracle: sql = $" SELECT rule_config.*,brand.`Name` BrandEntityName FROM rule_config LEFT JOIN brand ON rule_config.BrandId=brand.Id WHERE REGEXP_LIKE(@vin, RegularExpression)"; break;
                case DbType.PostgreSQL: sql = $"SELECT rule_config.*,brand.`Name` BrandEntityName FROM rule_config LEFT JOIN brand ON rule_config.BrandId=brand.Id WHERE @vin ~ RegularExpression"; break;
                case DbType.Sqlite: sql = $"SELECT rule_config.*,brand.`Name` BrandEntityName FROM rule_config LEFT JOIN brand ON rule_config.BrandId=brand.Id WHERE @vin REGEXP  RegularExpression"; break;
                default: sql = $"SELECT rule_config.*,brand.`Name` BrandEntityName FROM rule_config LEFT JOIN brand ON rule_config.BrandId=brand.Id WHERE @vin LIKE   RegularExpression"; break;
            }
            return await sqlSugarClient.Ado.SqlQueryAsync<RepRuleConfig>(sql, new { vin = VIN });           
        }

        /// <summary>
        /// 获取所有列表
        /// </summary>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async Task<List<RepRuleConfig>> GetListAsync(System.Linq.Expressions.Expression<Func<RuleConfigEntity, bool>> where = null, System.Linq.Expressions.Expression<Func<RuleConfigEntity, object>> order = null)
        {
            return await sqlSugarClient.Queryable<RuleConfigEntity>()
           .LeftJoin<BrandEntity>((m, b) => m.BrandId == b.Id)
          .WhereIF(where != null, where)
          .OrderByIF(order != null, (m, b) => order).Select<RepRuleConfig>().ToListAsync();
        }

        /// <summary>
        /// 分页查询 加入条件 和 排序
        /// </summary>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="current">当前第几页</param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async Task<PageModel<RepRuleConfig>> GetOrderPagesAsync(int pageSize, int current, System.Linq.Expressions.Expression<Func<RuleConfigEntity, bool>> where, System.Linq.Expressions.Expression<Func<RuleConfigEntity, object>> order = null, bool OrderByAsc = true)
        {

            RefAsync<int> totalCount = 0;//REF和OUT不支持异步,想要真的异步这是最优解
            OrderByType type = OrderByType.Asc;
            if (!OrderByAsc)
                type = OrderByType.Desc;
            var list = await sqlSugarClient.Queryable<RuleConfigEntity>()
           .LeftJoin<BrandEntity>((m, b) => m.BrandId == b.Id)
          .WhereIF(where != null, where)
           .OrderByIF(order != null,order, type).Select<RepRuleConfig>().ToPageListAsync(current, pageSize, totalCount);
            return new PageModel<RepRuleConfig>() { Data = list, TotalCount = totalCount, TotalPage = (int)Math.Ceiling(totalCount / (double)pageSize) };
        }

    }
}
